package com.aiju.cash.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.ShopClerk;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.LoginApi;
import com.aiju.utils.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-12-19 17:52
 * @version: V1.0
 * @Description: 店员管理
 */
public class ClerkManagerActivity extends BaseTitleActivity {

    private ListView clerkListView;

    private List<ShopClerk> clerks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clerk_manager);
        setCenterTitle("店员管理");
        setRightTitle("添加店员");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ClerkAddActivity.class);
            }
        });

        clerkListView = (ListView) findViewById(R.id.lv_clerk);
        clerkListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ClerkManagerActivity.this, ClerkInfoActivity.class);
                intent.putExtra("clerk", clerks.get(position));
                startActivity(intent);
            }
        });
        requestClerk();
    }

    private void requestClerk() {
        mDialogHelper.showLoadingProgressDialog();
        new LoginApi().userlist(mUser.getShop().getStoreId(), mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
                mDialogHelper.dismissProgressDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson json = new DataJson(response);
                if (json.isSuccess()) {
                    clerks = new Gson().fromJson(json.getData().toString(), new TypeToken<List<ShopClerk>>() {
                    }.getType());
                    clerkListView.setAdapter(new MyBaseAdapter(ClerkManagerActivity.this, clerks));
                } else {
                    T.ToastShort(json.getMsg());
                }
            }
        });
    }

    class MyComparator implements Comparator<ShopClerk> {
        public int compare(ShopClerk o1,ShopClerk o2) {
            if(Integer.valueOf(o1.getRole()) > Integer.valueOf(o2.getRole())){
                return 1;
            }
            if(Integer.valueOf(o1.getRole()) == Integer.valueOf(o2.getRole())){
                return 0;
            }
            return -1;
        }
    }

    private final class MyBaseAdapter extends BaseAdapter {

        private Context context;
        private List<ShopClerk> clerks;

        public MyBaseAdapter(Context context, List<ShopClerk> clerks) {
            this.context = context;
            this.clerks = clerks;
            Collections.sort(clerks,new MyComparator());
        }

        @Override
        public int getCount() {
            return clerks.size();
        }

        @Override
        public Object getItem(int position) {
            return clerks.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.item_clerk, parent, false);

            TextView name = (TextView) rootView.findViewById(R.id.tv_clerk_name);
            TextView id = (TextView) rootView.findViewById(R.id.tv_clerk_id);
            ImageView type = (ImageView) rootView.findViewById(R.id.iv_clerk_type);

            ShopClerk clerk = clerks.get(position);
            name.setText("昵称：" + clerk.getUserName());
            id.setText("账号：" + clerk.getPhone());
            if (!clerk.isClerk()) {
                type.setImageResource(R.mipmap.icon_user_admin);
            } else {
                type.setImageResource(R.mipmap.icon_user_clerk);
            }
            return rootView;
        }
    }
}
